package com.sure.supermarketserver.stock.supplier.impl;

import com.sure.supermarketserver.common.Utils;
import com.sure.supermarketserver.stock.supplier.SupplierDto;
import com.sure.supermarketserver.stock.supplier.SupplierService;
import com.sure.supermarketserver.stock.supplier.dao.SupplierDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

@Service
@Transactional
public class SupplierServiceImpl implements SupplierService {

    @Resource
    private SupplierDao supplierDao;

    @Override
    public Map<String, Object> findSupplierList(SupplierDto dto) {
        return Utils.getPage(dto, () -> supplierDao.findSupplierList(dto));
    }

    @Override
    public void addSupplier(SupplierDto dto) {
        try {
            boolean hassupplier = supplierDao.findSupplier(dto.getSu_id());
            if (hassupplier) {
                throw new RuntimeException("供货商编号已存在");
            }
            supplierDao.insertSupplier(dto);
        } catch (Exception e) {
            if (e instanceof RuntimeException) {
                throw e;
            }
            throw new RuntimeException("新增供货商失败！", e);
        }
    }

    @Override
    public void delSupplier(String id) {
        supplierDao.deleteSupplier(id);
    }

    @Override
    public void updSupplier(SupplierDto dto) {
        supplierDao.updateSupplier(dto);
    }
}
